package com.weiz.controller;

import com.weiz.model.LoginUser;
import com.weiz.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author weiz
 * @version 1.0
 * @create 2021/2/11 0:10
 */
@RestController
public class TokenTestController {
    @Autowired
    TokenUtil tokenUtil;

    @PostMapping("/login")
    public String login(@RequestBody LoginUser user){
        // 先验证用户的账号密码,账号密码验证通过之后，生成Token
        String role = "ROLE_ADMIN";
        String token = tokenUtil.getToken(user.username,role);
        return token;
    }

    @PostMapping("/testToken")
    public String testToken(HttpServletRequest request){
        String token = request.getHeader("token");
        tokenUtil.parseToken(token);
        return "请求成功";
    }
}
